package cn.bone.gateway.demo.web;

import cn.bone.common.api.demo.request.DemoUserLoginRo;
import cn.bone.common.api.demo.response.DemoUserHomePageVo;
import cn.bone.common.api.demo.response.DemoUserLoginVo;
import cn.bone.common.api.demo.service.IUserService;
import cn.bone.common.http.CommonHeader;
import cn.bone.common.http.CommonRequest;
import cn.bone.common.http.CommonResult;
import cn.bone.common.utils.JSONUtil;
import cn.bone.gateway.config.SpringFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import com.esotericsoftware.reflectasm.MethodAccess;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by superbone on 2017/4/23.
 */
@RestController
@RequestMapping("/demoUser")
public class UserController {

    @Resource(name="userService")
    IUserService userService;

    @RequestMapping("/login")
    public CommonResult<?> login(){

        CommonRequest<DemoUserLoginRo> request = new CommonRequest<>();
        CommonHeader header = new CommonHeader();
        header.setUserId(1);
        header.setFrom("test");
        header.setMethod("demoUser.login");
        request.setHeader(header);

        DemoUserLoginRo ro = new DemoUserLoginRo();
        ro.setUsername("superbone");
        ro.setPassword("123456");
        request.setParam(ro);

        CommonResult<DemoUserLoginVo> result = userService.login(request);
        return result;
    }

    @RequestMapping("/login2")
    public CommonResult<?> login2() throws Exception {


        CommonRequest<DemoUserLoginRo> request = new CommonRequest<>();
        CommonHeader header = new CommonHeader();
        header.setUserId(1);
        header.setFrom("test");
        header.setMethod("demoUser.login");
        request.setHeader(header);

        DemoUserLoginRo ro = new DemoUserLoginRo();
        ro.setUsername("superbone");
        ro.setPassword("123456");
        request.setParam(ro);
        String json = JSON.toJSONString(request);


        String className = "cn.bone.common.api.demo.service.IUserService";
        String beanName = "userService";
        String methodName = "login";
        Object bean = SpringFactory.getBean(beanName,Class.forName(className));
        Class clazz = Class.forName(className);
        MethodAccess methodAccess = MethodAccess.get(clazz);

        Class actualType = Class.forName("cn.bone.common.api.demo.request.DemoUserLoginRo");

        Type[] actualTypeArguments = {actualType};
        Type rawType = CommonRequest.class;
        ParameterizedType parameterizedType = new ParameterizedTypeImpl(
                actualTypeArguments,
                null,
                rawType);
        Object o = JSON.parseObject(json, parameterizedType);
        Object invoke = methodAccess.invoke(bean, methodName, o);
        CommonResult<?> result = (CommonResult<?>) invoke;
        return result;
    }

    @RequestMapping("/homePage")
    public CommonResult<?> homePage(){

        CommonRequest<Void> request = new CommonRequest<>();
        CommonHeader header = new CommonHeader();
        header.setUserId(1);
        header.setFrom("test");
        header.setMethod("demoUser.login");
        request.setHeader(header);
        CommonResult<DemoUserHomePageVo> result = userService.homePage(request);
        return result;
    }

    @RequestMapping("/homePage2")
    public CommonResult<?> homePage2() throws Exception {

        CommonRequest<Void> request = new CommonRequest<>();
        CommonHeader header = new CommonHeader();
        header.setUserId(1);
        header.setFrom("test");
        header.setMethod("demoUser.login");
        request.setHeader(header);
        String json = JSON.toJSONString(request);

        String className = "cn.bone.common.api.demo.service.IUserService";
        String beanName = "userService";
        String methodName = "homePage";
        Object bean = SpringFactory.getBean(beanName,Class.forName(className));
        Class clazz = Class.forName(className);
        MethodAccess methodAccess = MethodAccess.get(clazz);

        Type rawType = CommonRequest.class;
        Type[] actualTypeArguments = {Void.class};
        ParameterizedType parameterizedType = new ParameterizedTypeImpl(
                actualTypeArguments,
                null,
                rawType);

        Object o = JSON.parseObject(json, parameterizedType);
        Object invoke = methodAccess.invoke(bean, methodName, o);
        CommonResult<?> result = (CommonResult<?>) invoke;
        return result;
    }
}
